<?php
namespace app\shop\model\order;

use app\common\model\car\Car;
use app\common\model\order\CarOrderPlan;
use app\common\model\order\CarOrder as OrderModel;
use app\shop\model\shop\User;


/**
 * 订单模型
 */
class CarOrder extends OrderModel
{

    protected $pk = 'id';
    public function user()
    {
        return $this->belongsTo(User::Class);
    }

    public function car()
    {
        return $this->hasOneThrough(Car::Class,CarOrderPlan::class);
    }

    public static function get_month_order($params)
    {
        $model = new static();
        //$where[] = ['co.status','=',$params['status']];
        $model = $model->where(['co.status','=',$params['status']]);
        if($params['reg_date']){
            $timestamp = strtotime( $params['reg_date'] );
            $start_time = strtotime(date( 'Y-m-1 00:00:00', $timestamp ));
            $mdays = date( 't', $timestamp );
            $end_time = strtotime(date( 'Y-m-' . $mdays . ' 23:59:59', $timestamp ));

            $model = $model->whereTime('create_time', 'between', [$start_time,$end_time]);

            //$where[] = ['co.create_time','between',[$start_time,$end_time]];
        }else{

            $date = date("Y-m-d");
            $start_time = strtotime(date('Y-m-01 00:00:00', strtotime($date)));

            $end_time = strtotime(date('Y-m-d 23:59:59', strtotime("$start_time +1 month -1 day"))); //本月最后一天
            $model = $model->whereTime('create_time', 'between', [$start_time,$end_time]);
            //$where[] = ['co.create_time','between',[$start_time,$end_time]];
            return $model->with(['user'])->where('is_delete', '=', '0')
                ->order(['create_time' => 'desc'])
                ->hidden(['open_id', 'union_id'])
                ->paginate($params, false, [
                    'query' => \request()->request()
                ]);

        }
    }

    /*
     * 获取
     * */
    public function get_order_list($param)
    {
        $model = new static();
        if($param['status']){
            $model->where(['status','=',$param['status']]);
        }
        if($param['order_no']){
            $model->where(['order_no','=',$param['order_no']]);
        }

        if($param['reg_date']){
            $reg_date = $param['reg_date'];
            if (!empty($reg_date[0])) {
                if ($reg_date[0] == $reg_date[1]){
                    $reg_date[0] = $reg_date[0].' 00:00:00';
                    $reg_date[1] = $reg_date[1].' 23:59:59';
                }
                $model = $model->whereTime('create_time', 'between', $reg_date);
            }
        }

        return $model->with(['user','car'])->where('is_delete', '=', '0')
            ->order(['create_time' => 'desc'])
            //->hidden(['open_id', 'union_id'])
            ->paginate($param, false, [
                'query' => \request()->request()
            ]);
    }
}